package c.j.a.d.b.k;

import android.os.Process;
import c.j.a.d.b.e.b;
import java.io.InputStream;
import java.util.concurrent.Future;

/* compiled from: AsyncStreamReader */
public class a implements c {
    public final InputStream a;
    public final int b;
    public final int c;
    public final Object d = new Object();
    public final Object e = new Object();
    public b f;
    public b g;
    public b h;
    public b i;
    public b j;
    public b k;
    public volatile boolean l;
    public volatile boolean m;
    public volatile Throwable n;
    public volatile Future o;
    public int p;
    public final Runnable q;

    /* compiled from: AsyncStreamReader */
    public class a implements Runnable {
        public void run() {
            Process.setThreadPriority(10);
            b b;
            do {
                try {
                    b = a.b(a.this);
                    b.b = a.this.a.read(b.a);
                    a aVar = a.this;
                    synchronized (aVar.e) {
                        b bVar = aVar.j;
                        if (bVar == null) {
                            aVar.j = b;
                            aVar.i = b;
                            aVar.e.notify();
                        } else {
                            bVar.c = b;
                            aVar.j = b;
                        }
                    }
                } catch (Throwable th) {
                    try {
                        a.this.n = th;
                        th.printStackTrace();
                        synchronized (a.this.e) {
                            a.this.m = true;
                            a.this.e.notify();
                            c.j.a.d.b.n.a.q(a.this.a);
                            return;
                        }
                    } catch (Throwable th2) {
                        synchronized (a.this.e) {
                            a.this.m = true;
                            a.this.e.notify();
                            c.j.a.d.b.n.a.q(a.this.a);
                        }
                    }
                }
            } while (b.b != -1);
            synchronized (a.this.e) {
                a.this.m = true;
                a.this.e.notify();
            }
            c.j.a.d.b.n.a.q(a.this.a);
        }
    }

    public a(InputStream inputStream, int i, int i2) throws Throwable {
        a aVar = new a();
        this.q = aVar;
        this.a = inputStream;
        this.b = i;
        if (i2 < 1) {
            i2 = 1;
        } else if (i2 > 64) {
            i2 = 64;
        }
        this.c = i2;
        this.o = b.t().submit(aVar);
    }

    public void a(b bVar) {
        synchronized (this.d) {
            b bVar2 = this.g;
            if (bVar2 == null) {
                this.g = bVar;
                this.f = bVar;
                this.d.notify();
            } else {
                bVar2.c = bVar;
                this.g = bVar;
            }
        }
    }

    public void b() {
        synchronized (this.d) {
            this.l = true;
            this.d.notify();
        }
        Future future = this.o;
        if (future != null) {
            try {
                future.cancel(true);
            } catch (Throwable unused) {
            }
            this.o = null;
        }
    }

    public b a() throws com.ss.android.socialbase.downloader.e.a, InterruptedException {
        b bVar = this.k;
        if (bVar != null) {
            this.k = bVar.c;
            bVar.c = null;
            return bVar;
        }
        b bVar2;
        synchronized (this.e) {
            bVar2 = this.i;
            if (bVar2 == null) {
                while (!this.m) {
                    this.e.wait();
                    bVar2 = this.i;
                    if (bVar2 != null) {
                    }
                }
                Throwable th = this.n;
                if (th == null) {
                    throw new com.ss.android.socialbase.downloader.e.a(1069, "async reader terminated!");
                } else if (th instanceof d) {
                    throw new com.ss.android.socialbase.downloader.e.a(1068, "async reader closed!");
                } else {
                    c.j.a.d.b.n.a.o(th, "async_read");
                    throw null;
                }
            }
            this.k = bVar2.c;
            this.j = null;
            this.i = null;
            bVar2.c = null;
        }
        return bVar2;
    }

    public static b b(a aVar) throws d, InterruptedException {
        b bVar = aVar.h;
        if (bVar == null) {
            synchronized (aVar.d) {
                if (aVar.l) {
                    throw new d();
                }
                b bVar2 = aVar.f;
                if (bVar2 == null) {
                    int i = aVar.p;
                    if (i < aVar.c) {
                        aVar.p = i + 1;
                        b bVar3 = new b(aVar.b);
                        return bVar3;
                    }
                }
                while (bVar2 == null) {
                    aVar.d.wait();
                    if (aVar.l) {
                        throw new d();
                    }
                    bVar2 = aVar.f;
                }
                aVar.h = bVar2.c;
                aVar.g = null;
                aVar.f = null;
                bVar2.c = null;
                return bVar2;
            }
        } else if (aVar.l) {
            throw new d();
        } else {
            aVar.h = bVar.c;
            bVar.c = null;
            return bVar;
        }
    }
}
